package com.product.android.ui.adapter;


import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;

/**
 * 分页加载listview adapter
 * 
 * <br>Created 2014-5-9 上午11:02:58
 * @version  
 * @param <T>
 * @author   chenqsh		
 *
 * @see 	 @param <T>
 */
public abstract class BaseListViewAdapter extends BaseAdapter {
	protected static final int TYPE_PHOTOWALL_ITEM = 0;
	protected static final int TYPE_HR_RECOGNIZE_ITEM = 1;
	protected static final int TYPE_MAX_COUNT = TYPE_HR_RECOGNIZE_ITEM + 1;
	public TreeSet mHRRecognizeSet = new TreeSet();
	protected Context context;
	protected ArrayList<Object> mDataList = new ArrayList<Object>();

	private int scrollState = OnScrollListener.SCROLL_STATE_IDLE;

	protected int gridItemNum = 0;

	public BaseListViewAdapter(Context context, ArrayList<Object> arrays) {
		this.context = context;
		if (arrays != null) {
			this.mDataList.addAll(arrays);
		}
	}

	public BaseListViewAdapter(Context context, ArrayList<Object> arrays, int num) {
		this.context = context;
		if (arrays != null) {
			this.mDataList.addAll(arrays);
		}
		this.gridItemNum = num;
	}
	@Override
	public int getItemViewType(int position) {
		return mHRRecognizeSet.contains(position) ? TYPE_HR_RECOGNIZE_ITEM : TYPE_PHOTOWALL_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}
	
	public void addItemList(final ArrayList<?> dataList) {
		if (null != dataList && null != mDataList) {
			mDataList.addAll(dataList);
	        notifyDataSetChanged();
		}
	}
	public void setHRRecognizeItemFlag() {
		if (null != mDataList) {
			mHRRecognizeSet.add(mDataList.size());
		}
    }
	public void clearDataList() {
		if (null != mDataList) {
			mDataList.clear();
		}
	}

	public ArrayList<Object> getDataList() {
		return mDataList;
	}
	public void setDataList(ArrayList<Object> dataList) {
		if (null != mDataList) {
			mDataList.clear();
			mDataList.addAll(dataList);
		}
	}
	@Override
	public int getCount() {
		return null == mDataList ? 0 : mDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return null == mDataList ? null : mDataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	abstract public View getView(int position, View convertView,
			ViewGroup viewGroup);


	/**
	 * 在关闭页面的时候需要把临时变量清空掉.
	 */
	public void clearAdapter() {
		this.mDataList = null;
		this.context = null;
	}

	// 封装调整后的end参数
	protected int getEnd(int end) {
		return end > mDataList.size() ? mDataList.size() : end;
	}

	// 判断滚动是否处于停止状态
	protected boolean isScrollStateIdle() {
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			return true;
		}
		return false;
	}

	public void setScrollState(int state) {
		scrollState = state;
	}

	public int getScrollState() {
		return scrollState;
	}

	public void setIsEditMode(boolean isEditMode) {

	}

	public int getGridItemNum() {
		return gridItemNum;
	}
}
